package com.zxq.exchange.register;


import com.zxq.exchange.model.ExchangeDTO;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 通知设备，有消息下发
 */
public class NotifyDeviceRegister {

    static NotifyDeviceRegister notifyDeviceRegister = new NotifyDeviceRegister();

    /**
     * 获取注册者
     * @return
     */
    public static NotifyDeviceRegister getInstance() {

        return notifyDeviceRegister;
    }

    ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(2,8,30L, TimeUnit.SECONDS,new ArrayBlockingQueue<>(10));
    /**
     * 消息注册监听
     */
    List<INotifyDevice> registerCenter = new ArrayList<>();

    /**
     * 注册监听服务
     * @param listen
     */
    public void registerNotify(INotifyDevice listen){
        registerCenter.add(listen);
    }

    /**
     * 消息提醒
     */
    public void notifyMessage(ExchangeDTO exchangeDTO) {
        threadPoolExecutor.execute(() ->{
            registerCenter.forEach(listen -> {
                listen.notifyDevice(exchangeDTO);
            });
        });
   }









}
